﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using fuzhu;
using Dapper;
using LKU8.shoukuan.Model;

namespace LKU8.shoukuan
{
    public class InvService
    {
       

       /// <summary>
        /// 读取产品编码，根据产品类型
       /// </summary>
       /// <param name="clx">SMT DIP 或者后道</param>
       /// <param name="constr"></param>
       /// <returns></returns> 
        public static List<InvLoopUpEntity> GetList(string clx, string constr)
        {
            using (SqlConnection conn = new SqlConnection(constr))
            {
                string cCondition = "";
                if (clx == "SMT")
                {
                    cCondition = "  and (cinvcode like 'YA%'  OR cinvcode like 'YF%')";
                }
                else if (clx == "DIP")
                {
                    cCondition = "  and (cinvcode like 'WF%' cinvcode like 'ZF%')";
                }
                else if (clx == "后道")
                {
                    cCondition = "  and (cinvcode like 'YH%')";
                }

                //只取末级，and bDepEnd = 1。 查询条件里，不需要这个
                string sql = @"select  cInvCode 存货编码,cInvName 存货名称,cInvStd 规格型号 from inventory where 1= 1 " + cCondition;
                List<InvLoopUpEntity> lstdep = conn.Query<InvLoopUpEntity>(sql, null).ToList();
                return lstdep;
            }
        }

        /// <summary>
        /// 判断存货编码是否存在
        /// </summary>
        /// <param name="cInvCode">存货编码</param>
        /// <param name="constr">数据库连接字符串</param>
        /// <returns>存货编码是否存在</returns>
        public static bool IsInvCodeExists(string cInvCode, string constr)
        {
            using (SqlConnection conn = new SqlConnection(constr))
            {
                string sql = @"SELECT COUNT(1) FROM inventory WHERE cInvCode = @cInvCode";
                int count = conn.Query<int>(sql, new { cInvCode = cInvCode }).FirstOrDefault(); ; 
                return count > 0 ? true : false; 
            }
        }


        /// <summary>
        /// 查询存货编码实体
        /// </summary>
        /// <param name="cInvCode">存货编码</param>
        /// <param name="constr">数据库连接字符串</param>
        /// <returns>存货编码是否存在</returns>
        public static InventoryEntity GetEntity(string cInvCode, string constr)
        {
            using (SqlConnection conn = new SqlConnection(constr))
            {
                string sql = @"SELECT cInvCode,cInvName,cInvStd,cDefWareHouse,cInvDepCode FROM inventory WHERE cInvCode = @cInvCode";
                InventoryEntity inv = conn.Query<InventoryEntity>(sql, new { cInvCode = cInvCode }).FirstOrDefault(); ;
                return inv;
            }
        }
          
        /// <summary>
        /// 判断BOM是否存在
        /// </summary>
        /// <param name="cInvCode">存货编码</param>
        /// <param name="constr">数据库连接字符串</param>
        /// <returns>存货编码是否存在</returns>
        public static bool IsBOMExists(string cInvCode, string constr)
        {
            using (SqlConnection conn = new SqlConnection(constr))
            {
                string sql = @"select count(*) from bom_bom a,bom_parent b ,bas_part c
where a.BomId = b.BomId and b.ParentId =c.PartId
and c.InvCode= @cInvCode";
                int count = conn.Query<int>(sql, new { cInvCode = cInvCode }).FirstOrDefault(); ; 
                return count > 0 ? true : false; 
            }
        }

        /// <summary>
        /// 查询非后道的存货编码 根据机种名称
        /// </summary>
        /// <param name="cInvCode">存货编码</param>
        /// <param name="constr">数据库连接字符串</param>
        /// <returns>存货编码</returns>
        public static string GetInvCodeByJiZhong(string cJiZhong, string constr)
        {
            using (SqlConnection conn = new SqlConnection(constr))
            {
                string sql = @"SELECT cInvCode  FROM inventory_extradefine WHERE cidefine1 = @cJiZhong  ";
                string inv = conn.Query<string>(sql, new { cJiZhong = cJiZhong }).FirstOrDefault(); ;
                return inv;
            }
        }

        /// <summary>
        /// 查询后道的存货编码 根据机种名称
        /// </summary>
        /// <param name="cInvCode">存货编码</param>
        /// <param name="constr">数据库连接字符串</param>
        /// <returns>存货编码</returns>
        public static string GetInvCodeByJiZhong_HouDao(string cJiZhong, string constr)
        {
            using (SqlConnection conn = new SqlConnection(constr))
            {
                string sql = @"SELECT cInvCode  FROM inventory_extradefine WHERE cidefine1 = @cJiZhong and cinvcode like 'YH%' ";
                string inv = conn.Query<string>(sql, new { cJiZhong = cJiZhong }).FirstOrDefault(); ;
                return inv;
            }
        }
     }


         

       
 
}
